#import "ArcProgressView.h"

@implementation ArcProgressView {
    CAShapeLayer *_backgroundLayer;
    CAShapeLayer *_progressLayer;
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        _radius = 60;
        _percent = 0.0;
        _startAngle = -90;
        _sweepAngle = 270;
        _lineWidth = 10;
        
        self.backgroundColor = [UIColor clearColor];

        // 底部圆弧
        _backgroundLayer = [CAShapeLayer layer];
        _backgroundLayer.strokeColor = [UIColor hexColor:@"#005BE0"].CGColor;
        _backgroundLayer.fillColor = [UIColor clearColor].CGColor;
        _backgroundLayer.lineCap = kCALineCapRound;
        [self.layer addSublayer:_backgroundLayer];

        // 进度圆弧
        _progressLayer = [CAShapeLayer layer];
        _progressLayer.strokeColor = [UIColor hexColor:@"#D2E4FF"].CGColor;
        _progressLayer.fillColor = [UIColor clearColor].CGColor;
        _progressLayer.lineCap = kCALineCapRound;
        [self.layer addSublayer:_progressLayer];
        
        // Top Label
        _topLabel = [[UILabel alloc] init];
        _topLabel.textAlignment = NSTextAlignmentCenter;
        _topLabel.font = [UIFont systemFontOfSize:12 weight:(UIFontWeightRegular)];
        [self addSubview:_topLabel];
        _topLabel.textColor = UIColor.whiteColor;
        
        // Bottom Label
        _bottomLabel = [[UILabel alloc] init];
        _bottomLabel.textAlignment = NSTextAlignmentCenter;
        _bottomLabel.font = [UIFont systemFontOfSize:24 weight:(UIFontWeightMedium)];
        [self addSubview:_bottomLabel];
        _bottomLabel.textColor = UIColor.whiteColor;
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    CGPoint center = CGPointMake(CGRectGetWidth(self.bounds)/2, CGRectGetHeight(self.bounds)/2);
    
    UIBezierPath *arcPath = [UIBezierPath bezierPathWithArcCenter:center
                                                            radius:self.radius
                                                        startAngle:DEGREES_TO_RADIANS(self.startAngle)
                                                          endAngle:DEGREES_TO_RADIANS(self.startAngle + self.sweepAngle)
                                                         clockwise:YES];

    _backgroundLayer.path = arcPath.CGPath;
    _progressLayer.path = arcPath.CGPath;
    
    _backgroundLayer.lineWidth = self.lineWidth;
    _progressLayer.lineWidth = self.lineWidth;
    
    _backgroundLayer.frame = self.bounds;
    _progressLayer.frame = self.bounds;
    
    _progressLayer.strokeEnd = self.percent;
    
    CGFloat labelWidth = self.radius * 1.5;
    CGFloat labelHeight = 20;
    
    _topLabel.frame = CGRectMake((self.bounds.size.width - labelWidth)/2,
                                 center.y - labelHeight - 20,
                                 labelWidth,
                                 labelHeight);
    
    _bottomLabel.frame = CGRectMake((self.bounds.size.width - labelWidth)/2,
                                    _topLabel.bottom + 15,
                                    labelWidth,
                                    labelHeight);
}

- (void)updateProgress:(CGFloat)percent animated:(BOOL)animated {
    _percent = MIN(MAX(percent, 0.0), 1.0);

    if (animated) {
        CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
        anim.duration = 0.3;
        anim.fromValue = @(_progressLayer.presentationLayer.strokeEnd);
        anim.toValue = @(_percent);
        _progressLayer.strokeEnd = _percent;
        [_progressLayer addAnimation:anim forKey:@"progressAnim"];
    } else {
        _progressLayer.strokeEnd = _percent;
    }
}

static inline CGFloat DEGREES_TO_RADIANS(CGFloat degrees) {
    return degrees * M_PI / 180.0;
}

@end
